package com.wm.task1.server.dao;

import java.util.ArrayList;
import java.util.List;

public class ComputeDAO {
    public Integer compute(String equation) {
        equation = equation.replace(" ", "");
        List<Integer> equationList = new ArrayList<>();
        int countAdd = 0;
        int sum = 0;
        int temp = 0;
        for (int i = 0; i < equation.length() + 1; i++) {
            if (i == equation.length()) {
                equationList.add(temp);
                break;
            }
            if ((equation.charAt(i) > '9' || equation.charAt(i) < '0') && equation.charAt(i) != '+') {
                return null;
            }
            if (equation.charAt(i) == '+') {
                countAdd++;
                equationList.add(temp);
                temp = 0;
            } else if (temp == 0) {
                temp += equation.charAt(i) - '0';
            } else {
                temp *= 10;
                temp += equation.charAt(i) - '0';
            }
        }
        if (countAdd == 0) {
            return null;
        }
        for (int i = 0; i < equationList.size(); i++) {
            sum += equationList.get(i);
        }
        return sum;
    }
}
